package com.briup.srms.exam.service.impl;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotNull;

import com.briup.srms.exam.bean.StudentExam;
import com.briup.srms.sms.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.briup.srms.exam.bean.ClazzExam;
import com.briup.srms.exam.bean.Exam;
import com.briup.srms.exam.bean.ProfessorExam;
import com.briup.srms.exam.bean.extend.ExamExtend;
import com.briup.srms.exam.bean.extend.StudentExamExtend;
import com.briup.srms.exam.dao.ClazzExamMapper;
import com.briup.srms.exam.dao.ExamMapper;
import com.briup.srms.exam.dao.ProfessorExamMapper;
import com.briup.srms.exam.dao.StudentExamMapper;
import com.briup.srms.exam.dao.extend.ExamExtendMapper;
import com.briup.srms.exam.dao.extend.StudentExamExtendMapper;
import com.briup.srms.exam.service.IExamService;
import com.briup.srms.exam.vm.ExamVM;
import com.briup.srms.sms.bean.Clazz;
import com.briup.srms.sms.bean.Employee;
import com.briup.srms.sms.service.IStudentService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;
@Service
public class ExamServiceImpl implements IExamService {
	@Autowired
	private ExamMapper examMapper;
	@Autowired
	private ExamExtendMapper examExtendMapper;
	@Autowired
	private StudentExamMapper studentExamMapper;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ProfessorExamMapper professorExamMapper;
	@Autowired
	private ClazzExamMapper clazzExamMapper;
	@Autowired
	private StudentExamExtendMapper seeMapper;

	@Override
	public List<Exam> queryByStudentId(@NotNull Long studentId) {
		return examExtendMapper.selectByStudentId(studentId);
	}

	@Override
	@Transactional
	public ExamExtend queryById(@NotNull Long id) {
		ExamExtend exam = examExtendMapper.selectById(id);
		// 查询考试班级
		List<Clazz> clazzes = examExtendMapper.selectClazzesByExamId(id);
		// 查询考试阅卷老师
		List<Employee> professors = examExtendMapper.selectProfessorByExamId(id);
		exam.setClazzes(clazzes);
		exam.setProfessors(professors);
		return exam;
	}

	@Override
	public PageVM<Exam> pageQuery(int page, int pageSize) {
		List<Exam> list = examExtendMapper.query(page, pageSize);
		long total = examExtendMapper.count();
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	@Transactional
	public void saveOrUpdate(ExamVM examVM,List<Student> students) {
		Exam exam = examVM;
        System.out.println("exam_id:" + exam.getId());
		if(examVM.getId() == null) {
			// 创建考试
			exam.setCreateTime(new Date().getTime());
			exam.setStatus("正常");
			exam.setNumStudent(0);

			// exam.id没有被赋值
			examMapper.insert(exam);

			// 如果用户传递了clazzIds
			switch (exam.getExamType()) {
			case "阶段考试":
				Long[] clazzIds = examVM.getClazzIds();
				if( clazzIds !=null) {
					for(long clazzId : clazzIds) {
						// 根据班级id查询班级学生信息
						List<Student> list = studentService.queryByClazzId(clazzId);
						// 更新参与考试人数
						exam.setNumStudent(exam.getNumStudent()+list.size());
						examMapper.updateByPrimaryKey(exam);

						// 维护考试与班级之间的关系
						ClazzExam ce = new ClazzExam();
						ce.setClazzId(clazzId);
						ce.setExamId(exam.getId());

                        System.out.println("ce:" + ce);
						clazzExamMapper.insert(ce);

						// 维护考试与学生之间的关系
						if(list != null) {
							for(Student stu : list) {
								StudentExam se = new StudentExam();
								se.setStatus(StudentExamExtend.STATUS_UN_JOIN);
								se.setExamId(exam.getId());
								se.setStudentId(stu.getId());
								// 插入学生考试信息
								studentExamMapper.insert(se);
							}
						}
					}
				}
				break;
			case "招聘考试":
				// 更新参与考试人数
				exam.setNumStudent(exam.getNumStudent()+students.size());
				examMapper.updateByPrimaryKey(exam);
				// 维护考试与学生之间的关系
				if(students != null) {
					for(Student stu : students) {
						Student student = studentService.findByIdCard(stu.getIdCard());
						if(student == null) {
							throw new CustomerException("学生账号不存在:"+stu.getIdCard());
						}
						StudentExam se = new StudentExam();
						se.setStatus(StudentExamExtend.STATUS_UN_JOIN);
						se.setExamId(exam.getId());
						se.setStudentId(student.getId());
						// 插入学生考试信息
						studentExamMapper.insert(se);
					}
				}
				break;
			default:
				break;
			}
			// 维护考试与阅卷人之间的关系
			Long[] professorIds = examVM.getProfessorIds();
			if(professorIds!=null) {
				for(long professorId : professorIds) {
					ProfessorExam pe = new ProfessorExam();
					pe.setExamId(exam.getId());
					pe.setProfessorId(professorId);
					// 插入考试与阅卷人关系
					professorExamMapper.insert(pe);
				}
			}
		} else {
			examMapper.updateByPrimaryKey(exam);
		}
	}

	@Override
	public void deleteById(long id) {
		Exam exam = examMapper.selectByPrimaryKey(id);

		if(exam == null) {
			throw new CustomerException("要删除的数据不存在");
		}

		List<StudentExamExtend> list = seeMapper.select(1, 10000, StudentExamExtend.STATUS_FINISH, null, id, null);
		if(list.size()>0) {
			throw new CustomerException("该试卷下已经有已完成的案卷"+list.size()+"份");
		}
		examMapper.deleteByPrimaryKey(id);
	}

	@Override
	public StudentExamExtend queryStudentExam(@NotNull Long studentId, Long examId) {
		return seeMapper.query(studentId, examId);
	}

}
